package com.atguigu.aiproject.mapper;

import com.atguigu.aiproject.dto.CreationQueryDTO;
import com.atguigu.aiproject.entity.Creation;
import com.atguigu.aiproject.vo.CreationCoverVO;
import com.atguigu.aiproject.vo.CreationDetailsVO;
import com.atguigu.aiproject.vo.VecStoreCreationDetailVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CreationMapper extends BaseMapper<Creation> {
    @Select("${sql}")
    IPage<CreationCoverVO> selectBySql(Page<CreationCoverVO> page, String sql);


    IPage<CreationCoverVO> getCreationCoverVOList(Page<CreationCoverVO> page,
                                                  @Param("queryParam") CreationQueryDTO queryParam);

    @Select("""
            SELECT cgy.cgy_name AS cgyName, cre.s_id AS sId, cre.s_title AS sTitle,
             cre.s_click AS sClick, cre.s_keyword AS sKeyword, cre.s_createtime AS sCreateTime,
             cre.s_cover_img AS sCoverImg, cre.s_liked as sLiked,usr.u_id AS uId, usr.u_nickname AS uNickName
             FROM creations cre LEFT JOIN users usr ON cre.s_userid = usr.u_id
             LEFT JOIN cgys cgy ON cre.s_cgy = cgy.cgy_id WHERE usr.u_id = #{userId}""")
    IPage<CreationCoverVO> getCreationCoverVOListByUserId(Page<CreationCoverVO> page, int userId);


    CreationDetailsVO selectCreationDetailsById(int id);

    @Select("""
             SELECT cgy.cgy_name AS sCgyName, cre.s_id AS sId, cre.s_title AS sTitle,
             cre.s_content AS sContent, cre.s_click AS sClick, cre.s_keyword AS sKeyword,
             cre.s_createtime AS sCreateTime, cre.s_cover_img AS sCoverImg,
             usr.u_id AS uId, usr.u_nickname AS sUserNickName
             FROM creations cre LEFT JOIN users usr ON cre.s_userid = usr.u_id
             LEFT JOIN cgys cgy ON cre.s_cgy = cgy.cgy_id
            """
    )
    List<CreationDetailsVO> selectAllCreationDetails();

    @Update("UPDATE creations SET s_click = s_click + 1 WHERE s_id = #{id}")
    int incrementClickById(@Param("id") Integer id);

//    @Insert("INSERT INTO creations (s_title, s_content, s_cgy, s_userid, s_cover_img, s_keyword,s_click,s_createtime)" +
//            " VALUES (#{title}, #{content}, #{cgyId}, #{userId}, #{coverImg}, #{keyword},#{click},#{createTime})")
//    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "s_id")
//    void save(Creation creation);

    // todo
    IPage<CreationCoverVO> selectCreationCoverVOsByIds(Page<CreationCoverVO> page, List<Integer> creationIdList);

    @Update("UPDATE creations SET s_liked = s_liked + 1 WHERE s_id = #{creationId}")
    void incrementLikeCount(Integer creationId);

    @Update("UPDATE creations SET s_liked = s_liked - 1 WHERE s_id = #{creationId}")
    void decrementLikeCount(Integer creationId);

    @Update("UPDATE creations SET s_click = s_click + 1 WHERE s_id = #{creationId}")
    void incrementClickCount(Integer creationId);

    @Select("""
            SELECT cgy.cgy_name AS cgyName, cre.s_id AS creationId, cre.s_title AS title,
             cre.s_content AS content, cre.s_keyword AS keyword, usr.u_nickname AS userNickName
             FROM creations cre LEFT JOIN users usr ON cre.s_userid = usr.u_id
             LEFT JOIN cgys cgy ON cre.s_cgy = cgy.cgy_id""")
    List<VecStoreCreationDetailVO> selectAllVecStoreCreationDetails(); // 需要向量化的作品信息

    @Select("""
            SELECT cgy.cgy_name AS cgyName, cre.s_id AS creationId, cre.s_title AS title,
             cre.s_content AS content, cre.s_keyword AS keyword, usr.u_nickname AS userNickName
             FROM creations cre LEFT JOIN users usr ON cre.s_userid = usr.u_id
             LEFT JOIN cgys cgy ON cre.s_cgy = cgy.cgy_id
             WHERE cre.s_id = #{id}""")
    VecStoreCreationDetailVO selectVecStoreCreationDetailById(int id); // 用于向量化的作品信息（单个）
}
